package com.foreknow.dao.impl;

import com.foreknow.bean.Employee;
import com.foreknow.dao.EmployeeDao;

import java.sql.SQLException;

public class EmployeeDaoImpl implements EmployeeDao {
    private Employee[] emps;
    int count;

    public EmployeeDaoImpl() {
        emps = new Employee[3]; //创建一个数组容器用于保存员工对象
    }

    @Override
    public void add(Employee employee) throws SQLException {
        if(count < emps.length) {
            emps[count] = employee;
            count++;
            System.out.println("add success");
        }else {
            System.out.println("add error");
        }
    }

    @Override
    public Employee[] getQueryAll() {
        Employee[] employees = new Employee[3]; //定义一个新的容器
        for(int i = 0;i<emps.length;i++) {
            employees[i] = emps[i];
        }
        return employees;
    }

    @Override
    public Employee getQueryById(int id) {
        Employee e = null;
        for(int i = 0;i < emps.length;i++) {
            if(id == emps[i].getId()){
                e = emps[i];
                break;
            }
        }
        return e;
    }

    @Override
    public Employee update(Employee employee) {
        employee.setName("abc");
        return employee;
    }
}
